import { CoolLocalStorage } from '../../../../common/coolStorage/cool-local-storage'
import { CoolSessionStorage } from '../../../../common/coolStorage/cool-session-storage'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { Toast, Modal } from 'ng-zorro-antd-mobile'
import { ActivatedRoute, Router } from '@angular/router'
import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { TypeDetailService } from './services/type-detail.service'

@Component({
  selector: 'app-estate-type-detail',
  templateUrl: './estate-type-detail.component.html',
  styleUrls: ['./estate-type-detail.component.scss'],
  providers: [Modal]
})
export class EstateTypeDetailComponent implements OnInit {
  public loading: boolean = true
  public detail: any
  public layoutID: any
  public ishowApply: boolean = false

  items = ['楼盘', '活动', '动态', '户型', '点评']
  currentIndex = 0
  arroundMatching = [
    { alias: 'bus', icon: 'icon_bus.png', name: '公交' },
    { alias: 'school', icon: 'icon_school.png', name: '学校' },
    { alias: 'restaurant', icon: 'icon_restaurant.png', name: '餐饮' },
    { alias: 'shopping', icon: 'icon_shopping.png', name: '购物' },
    { alias: 'hospital', icon: 'icon_hospital.png', name: '医院' }
  ]
  estates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  constructor(
    private location: Location,
    private detailService: TypeDetailService,
    private routeInfo: ActivatedRoute,
    private router: Router,
    private commonUtil: CommonUtil,
    private coolLocalStorage: CoolLocalStorage,
    private _toast: Toast,
    private _modal: Modal
  ) {}

  ngOnInit() {
    this.layoutID = this.routeInfo.snapshot.queryParams['layout']
    this.getEstateDetail(this.layoutID)
  }

  //报名
  applyUp(item) {
    console.log(item)

    // this.ishowApply = true;
    this.router.navigate(['/workspace/home/activitySignup'], { queryParams: { ActivityID: item.Id } })
  }

  afterChange(number) {
    this.currentIndex = number
  }

  applyClose() {
    this.ishowApply = false
  }

  onClickLeft() {
    this.location.back()
  }

  //api
  getEstateDetail(id) {
    let reqdata = {
      layout: id,
      zone: this.coolLocalStorage.getItem('zone')
    }
    this.loading = true

    this.detailService.getEstateDetail(reqdata, res => {
      if (res.Success && res.Result) {
        let items = res.Result
        let photo: any = JSON.parse(items.Photo)

        let _p = parseInt(items.Price) / 10000
        console.log()

        if (_p < 1) {
          items.Price = '售价待定'
          items.Unit = ''
        } else {
          items.Price = Math.floor(_p / 10) * 10
          items.Unit = '万元'
        }
        items.Orientation = JSON.parse(items.Orientation)

        items.ProjectSummarys.forEach(element => {
          element.Photo = this.commonUtil.getThumbnail(element.Photo)

          if (typeof element.Tag === 'string') {
            element.Tag = JSON.parse(element.Tag)
          }
        })

        this.detail = items
        this.detail['Photos'] = []

        if (items.Tag) {
          this.detail['Tag'] = JSON.parse(items.Tag)
        }

        //相册
        photo.forEach(element => {
          console.log(element)

          if (element.Category) {
            element.Items.forEach(el => {
              let o = {
                src: el,
                Category: element.Category
              }
              this.detail.Photos.push(o)
            })
          }
        })
      }
      console.log(this.detail)
      this.loading = false
    })
  }
}
